/**
 * Copyright (c)2002-2006 by OpenSymphony
 * All rights reserved.
 */
package com.dev.security.manager.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;

import com.dev.security.manager.beans.Source;
import com.dev.security.manager.dao.SourceRepository;
import com.dev.security.manager.service.ISourceService;

/**
 * @author 潜行的青衣
 * @see
 */
@Service
public class SourceService implements ISourceService {

	@Autowired
	private SourceRepository sourceRepository;
	
	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.ISourceService#save(java.lang.String, java.lang.String)
	 */
	@Override
	public boolean save(String uri, String per_id) {
		return this.save(new Source(uri, per_id));
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.ISourceService#remove(java.lang.String[])
	 */
	@Override
	public void remove(String[] uris) {
		for(String uri : uris) {
			this.sourceRepository.delete(uri);
		}
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.ISourceService#findPager(java.lang.String, int, int)
	 */
	@Override
	public Page<Source> findPager(String per_id, int page, int size) {
		Sort sort = new Sort(new Order(Direction.ASC, "uri"));
		return this.sourceRepository.findAll(per_id, new PageRequest(page - 1, size, sort));
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.ISourceService#save(com.dev.security.manager.beans.Source)
	 */
	@Override
	public boolean save(Source source) {
		if(this.sourceRepository.count(source.getUri()) > 0) {
			return false;
		}
		this.sourceRepository.save(source);
		return true;
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.ISourceService#findPager(java.lang.String[], int, int)
	 */
	@Override
	public Page<Source> findPager(String[] per_ids, int page, int size) {
		Sort sort = new Sort(new Order(Direction.ASC, "uri"));
		return this.sourceRepository.findAll(per_ids, new PageRequest(page - 1, size, sort));
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.ISourceService#remove(java.lang.String[], java.lang.String[])
	 */
	@Override
	public void remove(String[] uris, String[] per_ids) {
		this.sourceRepository.delete(uris, per_ids);
	}

}
